package mainService.services;

import java.util.ArrayList;
import java.util.Collections;

import mainService.HTTPService;
import mainService.ServiceParams;
import mainService.data.HttpMessageWriter;
import mainService.data.IGeneratorXml;
import mainService.data.IpClient;
import mainService.data.MimeHTTP;
import mainService.data.UsersConnected;
import mainService.data.Utente;
import mainService.data.XMLGenerator;

/**
 * Servizio che permette di sapere le informazioni sulla sessione attiva
 * 
 * @author Daniel Procop, Mattia Grecchi
 */
public class SintesiDatiUtenteService extends ReadingServices implements
		HTTPService {

	static private ArrayList<String> campiXML = new ArrayList<String>();

	static {
		Collections.addAll(campiXML, "username", "role", "name");
	}

	@Override
	public ArrayList<String> getServiceResult(ServiceParams params) {

		if (!UsersConnected.getUsersConnected().ipPresente(
				IpClient.getSingleton().getIp())) {
			HttpMessageWriter writer = new HttpMessageWriter(MimeHTTP.XML);
			ArrayList<String> risposta = writer.createHttpMessageHeader();

			IGeneratorXml generatorXML = new XMLGenerator(risposta, campiXML);
			ArrayList<String> valoreCampi = new ArrayList<>();
			Collections.addAll(valoreCampi, "Guest", "-", "-");
			generatorXML.addElement(valoreCampi);
			return generatorXML.getRisposta();
		} else {
			Utente user = UsersConnected.getUsersConnected().getUtente(
					IpClient.getSingleton().getIp());
			String id_user = user.getUsername();

			String qry = "SELECT username, role, name from users, projects where username='"
					+ id_user + "' and users.id_project=projects.id_project";
			return getResult(qry, campiXML);
		}
	}

}
